package com.alex.springboot.controller;

import com.alex.springboot.entity.User;
import com.alex.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/save")
    public User save(@ModelAttribute User user) {
        userService.save(user);
        return user;
    }

    @DeleteMapping("/{id}")
    public Integer delete(@PathVariable Integer id) {
        userService.delete(id);
        return id;
    }

    @PutMapping("/update")
    public User update(@ModelAttribute User user) {
        userService.update(user);
        return user;
    }

    @GetMapping("/{id}")
    public User getById(@PathVariable Integer id) {
        return userService.getById(id);
    }

    @GetMapping("/all")
    public List<User> all() {
        return userService.selectAll();
    }

}
